<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="5" :xs="24">
        <el-card shadow="always">
          <org-tree ref="orgTree" modelType="'dept','community','floor','unit','room'" @nodeClick="handleNodeClick" @firstNodeClick="handleFirstNodeClick"/>
        </el-card>
      </el-col>
      <el-col :span="19" :xs="24">
        <el-card shadow="always">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="所属小区" prop="communityId">
        <el-select v-model="queryParams.communityId" placeholder="请选择所属小区">
          <el-option
            v-for="dict in communityOptions"
            :key="dict.id"
            :label="dict.communityName"
            :value="dict.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="维修员" prop="repairman">
        <el-input
          v-model="queryParams.repairman"
          placeholder="请输入维修员"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报修类型" prop="repairType">
        <el-select v-model="queryParams.repairType" placeholder="请选择报修类型" clearable size="small">
          <el-option
            v-for="dict in repairTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="报修项" prop="repairTerm">
        <el-select v-model="queryParams.repairTerm" placeholder="请选择报修项" clearable size="small">
          <el-option
            v-for="dict in repairTermOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="报修状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择报修状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['property:repairOrder:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['property:repairOrder:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['property:repairOrder:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['property:repairOrder:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="repairOrderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="小区名称" align="center" prop="communityName"/>
      <el-table-column label="报修人" align="center" prop="ownerName"/>
      <el-table-column label="报修内容" align="center" prop="repairContent"/>
      <el-table-column label="维修员" align="center" prop="repairman"/>
      <el-table-column label="报修类型" align="center" prop="repairType" :formatter="repairTypeFormat"/>
      <el-table-column label="报修项" align="center" prop="repairTerm" :formatter="repairTermFormat"/>
      <el-table-column label="报修状态" align="center" prop="status" :formatter="statusFormat"/>
      <el-table-column label="是否回访" align="center" prop="status" :formatter="statusFormat">
        <template slot-scope="scope">
          <span v-if="scope.row.status == 1">需要</span>
          <span v-if="scope.row.status == 2">不需要</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createBy"/>-->
    <!--  <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['property:repairOrder:edit']"
          >修改
          </el-button>
          <el-dropdown trigger="click">
            <el-button type="text" size="small" icon="el-icon-plus">更多</el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="1">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelete(scope.row)"
                  v-hasPermi="['property:owner:remove']"
                >删除
                </el-button>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
        </el-card>
        </el-col>
    </el-row>
    <!-- 添加或修改报修单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="960px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="20">
          <el-col :span="12" :xs="24">
            <el-form-item label="所属小区" prop="communityName">
              <el-select v-model="form.communityId" placeholder="请选择所属小区" :disabled="enableEdit" @change="getFloorList">
                <el-option
                  v-for="item in communityOptions"
                  :key="item.id"
                  :label="item.communityName"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" :xs="24">
            <el-form-item label="所属楼栋" prop="floorName">
              <el-select v-model="form.floorId" placeholder="请选择所属楼栋" :disabled="enableEdit" @change="getRoomList">
                <el-option
                  v-for="item in floorOptions"
                  :key="item.id"
                  :label="item.floorName"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12" :xs="24">
            <el-form-item label="所属房屋" prop="roomName">
              <el-select v-model="form.roomId" placeholder="请选择所属房间" :disabled="enableEdit">
                <el-option
                  v-for="item in roomOptions"
                  :key="item.id"
                  :label="item.roomName"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" :xs="24">
            <el-form-item label="是否回访" prop="needVisitMes">
              <el-select v-model="form.needVisitMes" :disabled="enableEdit" placeholder="请选择是否回访">
                <el-option
                  v-for="item in needReturnVisitSelect"
                  :key="item.needReturnVisit"
                  :label="item.label"
                  :value="item.needReturnVisit"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12" :xs="24">
            <el-form-item label="报修人" prop="ownerName">
              <el-input v-model="form.ownerName" :disabled="enableEdit"/>
            </el-form-item>
          </el-col>
          <el-col :span="12" :xs="24">
            <el-form-item label="服务类型" prop="serviceType">
              <treeselect
              :show-count="true"
              :default-expand-level="1"
              disable-branch-nodes="true"
              v-model="form.serviceType"
              :options="serviceDictOptions"
              :normalizer="normalizer"
              :disabled="enableEdit"
              placeholder="请选择服务类型" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12" :xs="24">
            <el-form-item label="维修员" prop="repairman">
              <el-input v-model="form.repairman" placeholder="请输入维修员"/>
            </el-form-item>
          </el-col>
          <el-col :span="12" :xs="24">
            <el-form-item label="报修来源" prop="repairType">
              <el-select v-model="form.repairType" placeholder="请选择报修类型">
                <el-option
                  v-for="dict in repairTypeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12" :xs="24">
            <el-form-item label="报修项" prop="repairTerm">
              <el-select v-model="form.repairTerm" placeholder="请选择报修项">
                <el-option
                  v-for="dict in repairTermOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" :xs="24">
            <el-form-item label="报修状态" prop="status">
              <el-select v-model="form.status" placeholder="请选择报修状态">
                <el-option
                  v-for="dict in statusOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24" :xs="24">
            <el-form-item label="报修内容">
              <el-input type="textarea" v-model="form.repairContent" :min-height="192"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listRepairOrder, getRepairOrder, delRepairOrder, addRepairOrder, updateRepairOrder, exportRepairOrder} from "@/api/property/repairOrder";
  import { listCommunity} from "@/api/property/community";
  import { listServiceDict } from "@/api/system/serviceDict";
  import { listFloor} from "@/api/property/floor";
  import { listRoom} from "@/api/property/room";
  import Treeselect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";

  export default {
    name: "RepairOrder",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 导出遮罩层
        exportLoading: false,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 报修单表格数据
        repairOrderList: [],
        serviceDictOptions: [],
        //小区列表
        communityOptions:[],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 报修类型字典
        repairTypeOptions: [],
        // 报修项字典
        repairTermOptions: [],
        // 报修状态字典
        statusOptions: [],
        needReturnVisitSelect:[
          {needReturnVisit: 1, label: "需要"},
          {needReturnVisit: 2, label: "不需要"}
        ],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          nodeId: null,
          communityId: null,
          repairContent: null,
          repairman: null,
          repairType: null,
          repairTerm: null,
          status: null,
        },
        queryFloorParam:{
          communityId: null
        },
        floorOptions: [],
        queryRoomParam:{
          floorId: null
        },
        roomOptions: [],
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          communityId: [
            {required: true, message: "所属小区不能为空", trigger: "change"}
          ],
          needReturnVisit: [
            {required: true, message: "是否回访不能为空", trigger: "change"}
          ],
          repairContent: [
            {required: true, message: "报修内容不能为空", trigger: "blur"}
          ],
          status: [
            {required: true, message: "报修状态不能为空", trigger: "change"}
          ]
        },
        enableEdit: false
      };
    },
    components:{
      Treeselect
    },
    created() {
      this.getList();
      this.getDicts("repair_type").then(response => {
        this.repairTypeOptions = response.data;
      });
      this.getDicts("repair_term").then(response => {
        this.repairTermOptions = response.data;
      });
      this.getDicts("repair_status").then(response => {
        this.statusOptions = response.data;
      });
      this.getCommunityOptionsList();
      this.serviceDictOptionsList();
    },
    methods: {
      /** 查询报修单列表 */
      getList() {
        this.loading = true;
        listRepairOrder(this.queryParams).then(response => {
          this.repairOrderList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      serviceDictOptionsList(){
        listServiceDict(this.queryParams).then(res => {
          this.serviceDictOptions = res.rows
        })
      },
      /** 查询服务类型数据字典下拉树结构 */
      getTreeselect() {
        listServiceDict().then(response => {
          this.serviceDictOptions = [];
          const data = { serviceCode: 0, serviceName: '顶级节点', children: [] };
          data.children = this.handleTree(response.data, "serviceCode", "parentCode");
          this.serviceDictOptions.push(data);
        });
      },
      /** 转换服务类型数据字典数据结构 */
      normalizer(node) {
        if (node.children && !node.children.length) {
          delete node.children;
        }
        return {
          id: node.serviceCode,
          label: node.serviceName,
          children: node.children
        };
      },
      getCommunityOptionsList(){
        listCommunity().then(response => {
          this.communityOptions =response.rows;
        })
      },
      getFloorList(){
        this.queryFloorParam.communityId = this.form.communityId
        listFloor(this.queryFloorParam).then(res => {
          this.floorOptions = res.rows
        })
      },
      getRoomList(){
        this.queryRoomParam.floorId = this.form.floorId
        listRoom(this.queryRoomParam).then(res => {
          this.roomOptions = res.rows
        })
      },
      // 报修类型字典翻译
      repairTypeFormat(row, column) {
        return this.selectDictLabel(this.repairTypeOptions, row.repairType);
      },
      // 报修项字典翻译
      repairTermFormat(row, column) {
        return this.selectDictLabel(this.repairTermOptions, row.repairTerm);
      },
      // 报修状态字典翻译
      statusFormat(row, column) {
        return this.selectDictLabel(this.statusOptions, row.status);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          repairContent: null,
          repairman: null,
          repairType: null,
          repairTerm: null,
          status: null,
          createTime: null,
          deptId:null,
          ancestors: null,
          createBy: null,
          updateTime: null,
          updateBy: null,
          isDeleted: null,
          serviceType: null,
          serviceTypeName: null,
          floorId: null,
          roomId: null,
          needVisitMes: null,
          serviceCode: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.getTreeselect();
        this.enableEdit = false
        this.open = true;
        this.title = "添加报修单";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        this.getTreeselect();
        this.enableEdit = true
        const id = row.id || this.ids
        getRepairOrder(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改报修单";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateRepairOrder(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addRepairOrder(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除报修单编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return delRepairOrder(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有报修单数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportRepairOrder(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {
        });
      },
      handleNodeClick(node){
        this.queryParams.nodeId = node.id
        this.getList()
      },
      handleFirstNodeClick(node){
        this.currentNode = node;
        this.queryParams.nodeId = node.id;
        this.getList();
      },
    }
  };
</script>
